package com.carkeeper.system.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.system.pojo.Role;
import com.carkeeper.system.service.RoleService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 周志雄
 * @version 1.0
 * @date 2020/11/24 14:42
 */
@RestController
@CrossOrigin
@RequestMapping("/role")
@Api(tags = "角色管理接口api", description = "角色管理接口，提供页面的增、删、改、查")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 条件+分页
     *
     * @param searchMap
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/{page}/{pagesize}")
    @ApiOperation(value = "queryByCondition", notes = "分页+条件查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "path"),
            @ApiImplicitParam(name = "pagesize", value = "每页显示条数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "name", value = "角色名称", required = false, paramType = "query")
    })
    public Result queryByCondition(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int pagesize) {
        Page<Role> result = roleService.queryByCondition(searchMap, page, pagesize);
        return new Result(true, StatusCode.OK, "查询成功", result);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "findAll", notes = "查询所有", httpMethod = "GET")
    public List<Role> findAll() {
        List<Role> roleList = roleService.findAll();
        return roleList;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "delete", notes = "删除角色", httpMethod = "DELETE")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path")
    )
    public Result delete(@PathVariable Long id) {
        Result result = roleService.delete(id);
        return result;
    }


    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping
    @ApiOperation(value = "add", notes = "添加角色", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, paramType = "body"),
            @ApiImplicitParam(name = "detail", value = "角色详情", required = true, paramType = "body"),
            @ApiImplicitParam(name = "menuIds", value = "所选择的菜单", required = true, paramType = "body")
    })
    public Result add(@RequestBody Role role) {
        Result result = roleService.add(role);
        return result;
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PutMapping
    @ApiOperation(value = "update", notes = "修改角色", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "body"),
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, paramType = "body"),
            @ApiImplicitParam(name = "detail", value = "角色详情", required = true, paramType = "body"),
            @ApiImplicitParam(name = "menuIds", value = "所选择的菜单", required = true, paramType = "body")
    })
    public Result update(@RequestBody Role role) {
        Result result = roleService.update(role);
        return result;
    }

}
